<template>
  <div class="bigbox">
    <div class="topbox" :style="{ height: isheight + 'px' }">
      <el-form
        :model="form"
        ref="form"
        label-position="left"
        label-width="80px"
        onsubmit="return false"
      >
        <el-col :span="6">
          <el-form-item label="用电企业">
            <el-select
              style="width: 100%"
              v-model="bizOrgNo"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in companyList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="企业代码">
            <el-input
              v-model="enterpriseCode"
              placeholder="请输入企业代码"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="开户日期">
            <el-date-picker
              v-model="dataList"
              style="width: 400px"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <div>
              <button @click="chongzhi" class="button">重置</button>
              <button @click="submit" class="button chaxun">查询</button>
            </div>
          </el-form-item>
        </el-col>
      </el-form>
    </div>
    <div class="bodybox">
      <div class="titlebox">
        <div class="text">客户档案</div>
        <div
          class="right"
          :style="{ alignItems: 'center', lineHeight: '32.5px' }"
        >
          <button
            type="primary"
            size="small"
            class="bewdao daochu"
            @click="add"
          >
            新增
          </button>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <span
              @click="shuaxin"
              :style="{ cursor: 'pointer', fontSize: '20px' }"
              ><i class="el-icon-refresh-right"></i
            ></span>
          </el-tooltip>
          <el-dropdown trigger="click" @command="command">
            <span class="el-dropdown-link">
              <el-tooltip
                class="item"
                effect="dark"
                content="密度"
                placement="top"
              >
                <span :style="{ cursor: 'pointer', fontSize: '20px' }"
                  ><i class="el-icon-d-caret"></i
                ></span>
              </el-tooltip>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item class="dropdown-list" command="默认"
                >默认</el-dropdown-item
              >
              <el-dropdown-item class="dropdown-list" command="中等"
                >中等</el-dropdown-item
              >
              <el-dropdown-item class="dropdown-list" command="紧凑"
                >紧凑</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <div class="table-content">
        <template>
          <el-table
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            border
            id="exportTabyong"
            ref="table"
            :size="iissize"
            :header-cell-style="{
              background: '#f5f5f5',
              'text-align': 'center'
            }"
          >
            <template slot="empty">
              <el-empty description="暂无数据"></el-empty>
            </template>
            <el-table-column
              type="index"
              align="center"
              label="序号"
              width="50"
            >
            </el-table-column>
            <el-table-column prop="enterpriseCode" label="户号" width="180">
            </el-table-column>
            <el-table-column prop="companyName" label="用电企业" width="200">
            </el-table-column>
            <el-table-column
              prop="createDate"
              align="center"
              label="开户日期"
              width="120"
            >
            </el-table-column>
            <el-table-column prop="address" label="用电地址" width="350">
            </el-table-column>
            <el-table-column
              prop="contractCapacity"
              label="合同容量"
              align="right"
              width="150"
            >
            </el-table-column>
            <el-table-column
              prop="runCapacity"
              align="right"
              label="运行容量"
              width="150"
            >
            </el-table-column>
            <el-table-column
              prop="industryCategoryName"
              label="行业类别"
              align="center"
              width="200"
            >
            </el-table-column>
            <el-table-column
              prop="industryTypeName"
              label="行业分类"
              align="center"
              width="200"
            >
            </el-table-column>
            <el-table-column
              prop="contacts"
              align="center"
              label="联系人"
              width="80"
            >
            </el-table-column>
            <el-table-column
              prop="contactsMobile"
              align="center"
              label="联系手机"
              width="200"
            >
            </el-table-column>
            <el-table-column
              prop="provinceName"
              align="center"
              label="省"
              width="150"
            >
            </el-table-column>
            <el-table-column
              prop="cityName"
              align="center"
              label="市"
              width="150"
            >
            </el-table-column>
            <el-table-column
              prop="areaName"
              align="center"
              label="区/县"
              width="150"
            >
            </el-table-column>
            <el-table-column prop="status" align="center" label="状态">
              <template slot-scope="scope">
                {{ statusList.find((i) => scope.row.status === i.value).text }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="130">
              <template slot-scope="scope">
                <div class="operate">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="预览"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-view"
                      @click="preview(scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="编辑"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-edit"
                      @click="handleEdit(scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="删除"
                    placement="top"
                  >
                    <i
                      style="font-size: 14px; color: red; cursor: pointer"
                      class="el-icon-delete"
                      @click="handleDel(scope.row.id)"
                    ></i
                  ></el-tooltip>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <div class="fenye">
          <el-pagination
            background
            layout="prev, pager, next"
            :current-page.sync="pageobj._pageNo"
            @current-change="currentChange"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="dialog">
      <div class="dialog">
        <el-dialog
          :title="''"
          :visible.sync="centerDialogVisible"
          label-position="left"
          center
          width="800px"
          :destroy-on-close="true"
          :close-on-click-modal="false"
          :before-close="beforeClose"
        >
          <div class="top-line">
            <div class="top-item">
              <div class="circle" :class="progress == 1 ? 'checked-bgc' : ''">
                1
              </div>
              <div class="text">账号信息</div>
            </div>
            <div class="line"></div>
            <div class="top-item">
              <div class="circle" :class="progress == 2 ? 'checked-bgc' : ''">
                2
              </div>
              <div class="text">业务范围</div>
            </div>
            <div class="line"></div>
            <div class="top-item">
              <div class="circle" :class="progress == 3 ? 'checked-bgc' : ''">
                3
              </div>
              <div class="text">公司信息</div>
            </div>
            <div class="line"></div>
            <div class="top-item">
              <div class="circle" :class="progress == 4 ? 'checked-bgc' : ''">
                4
              </div>
              <div class="text">地理位置</div>
            </div>
          </div>
          <div class="content">
            <AccountInfo
              ref="AccountInfo"
              @fileData="fileData"
              @next="next"
              :addType="0"
              v-if="progress == 1 && centerDialogVisible"
            />
            <BusinessScope
              @next="next"
              :id="id"
              :isEdit="isEdit"
              ref="BusinessScope"
              v-else-if="progress == 2 && centerDialogVisible"
            />
            <CompanyInfo
              @next="next"
              :id="id"
              :isEdit="isEdit"
              @fileData="fileData"
              :companyData="companyData"
              ref="CompanyInfo"
              v-else-if="progress == 3 && centerDialogVisible"
            />
            <Amap
              ref="amap"
              :id="id"
              :dizhi="dizhi"
              :isEdit="isEdit"
              @finish="finish"
              v-else-if="progress == 4 && centerDialogVisible"
            />
          </div>
        </el-dialog>
        <el-dialog
          :title="'公司注册信息'"
          :visible.sync="centerDialogVisible1"
          label-position="left"
          width="900px"
          :destroy-on-close="true"
          :close-on-click-modal="false"
          :before-close="beforeClose"
        >
          <div class="content">
            <AccountInfo
              ref="AccountInfo"
              :id="id"
              @next="next"
              @fileData="fileData"
              :addType="0"
              :isPreview="isPreview"
              :companyData="companyData"
              v-if="centerDialogVisible1"
            />
            <BusinessScope
              @next="next"
              :id="id"
              :isPreview="isPreview"
              :companyData="companyData"
              :isEdit="isEdit"
              ref="BusinessScope"
              v-if="centerDialogVisible1"
            />
            <CompanyInfo
              @next="next"
              :id="id"
              :isPreview="isPreview"
              :isEdit="isEdit"
              :companyData="companyData"
              ref="CompanyInfo"
              v-if="centerDialogVisible1"
            />
            <Amap
              ref="amap"
              :id="id"
              :dizhi="dizhi"
              :isPreview="isPreview"
              :isEdit="isEdit"
              :companyData="companyData"
              @finish="finish"
              v-if="centerDialogVisible1"
            />
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
// import { saveAs } from 'file-saver'
import {
  getCompanyListApi,
  delCustomerFileApi,
  getCustomerFileApi,
  getCustomerInfoApi
  // getCustomerInfoApi
} from '../../api/customerFile'
import Amap from './components/Amap.vue'
import AccountInfo from './components/AccountInfo.vue'
import BusinessScope from './components/BusinessScope.vue'
import CompanyInfo from './components/CompanyInfo.vue'
import dayjs from 'dayjs'
import { getEnterpriseData, getStatusValueApi } from '@/api/companyRegister'
export default {
  name: 'ConfigureIndex',
  components: { Amap, AccountInfo, BusinessScope, CompanyInfo },
  data() {
    return {
      // lng: '',
      // lat: '',
      // checkList: [],
      dataList: [],
      enterpriseCode: '',
      id: '',
      bizOrgNo: '',
      isEdit: false,
      dizhi: '',
      centerDialogVisible: false,
      centerDialogVisible1: false,
      form: { alarmLevel: '0' },
      statusList: [],
      // dialogForm: {
      //   province: '',
      //   city: '',
      //   area: '',
      //   createDate: ''
      // },
      address: '',
      isshow: false,
      isheight: 72,
      date: [],
      iissize: 'medium',
      isPreview: false,
      // HYFL: [],
      loading: false,
      tableData: [],
      total: 0,
      pageobj: {
        _pageNo: 0,
        _pageSize: 10
      },
      progress: 1,
      addType: '',
      companyData: {},
      companyList: []
    }
  },
  created() {
    this.getCompanyList()
    this.getStatusValue()
  },

  async mounted() {
    await this.getchushihua()
    await this.getdatalist('')
  },

  methods: {
    // 获取用电企业列表
    async getCompanyList() {
      const res = await getCompanyListApi()
      console.log(res)
      this.companyList = res.data.list
    },
    async getStatusValue() {
      const res = await getStatusValueApi()
      // console.log(res.data.list)
      this.statusList = res.data.list
    },
    // 初始化
    async getchushihua() {
      this.bizOrgNo = ''
    },
    // 获取数据
    async getdatalist(now) {
      this.loading = true
      const res = await getCustomerFileApi({
        _pageNo: this.pageobj._pageNo - 1,
        _pageSize: this.pageobj._pageSize,
        orgNo: now,
        enterpriseCode: this.enterpriseCode || '',
        startDate: this.dataList[0]
          ? dayjs(this.dataList[0]).format('YYYY-MM-DD')
          : '',
        endDate: this.dataList[1]
          ? dayjs(this.dataList[1]).format('YYYY-MM-DD')
          : ''
      })
      this.tableData = res.data.list
      this.total = res.data.totalCount
      console.log(res)
      // if (this.enterpriseCode) {
      //   this.tableData = this.tableData.filter((c) => {
      //     return c.enterpriseCode === this.enterpriseCode
      //   })
      // }
      // if (this.dataList.length > 0) {
      //   this.tableData = this.tableData.filter((item) => {
      //     return (
      //       this.dataList[1] > new Date(item.createDate) &&
      //       new Date(item.createDate) > this.dataList[0]
      //     )
      //   })
      // }
      this.loading = false
    },
    // 分页查询
    currentChange() {
      this.getdatalist(this.bizOrgNo)
    },
    // 刷新列表
    shuaxin() {
      this.getdatalist('')
      this.$message.success('列表刷新成功')
    },
    submit() {
      // console.log(this.dataList)
      this.getdatalist(this.bizOrgNo)
    },
    command(val) {
      if (val === '默认') {
        this.iissize = 'medium'
      } else if (val === '中等') {
        this.iissize = 'small'
      } else {
        this.iissize = 'mini'
      }
    },

    async chongzhi() {
      await this.getchushihua()
      this.date = []
      this.enterpriseCode = ''
      this.dataList = []
      this.getdatalist('')
      this.form.alarmLevel = '0'
    },
    // 编辑
    async handleEdit(row) {
      console.log(row)
      if (row.status == 4) return this.$message('创建已完成，不可编辑')
      this.progress = +row.status + 1
      this.id = row.id
      if (row.status == 2) {
        const res = await getEnterpriseData(row.companyName)
        this.companyData = res.data.data.data
        this.dizhi = res.data.data.data.companyAddress
      } else {
        this.dizhi = row.address
      }
      this.isEdit = true
      this.centerDialogVisible = true
    },
    async preview(row) {
      console.log(row)
      this.isPreview = true
      this.id = row.id
      const res = await getCustomerInfoApi(row.id)
      console.log(res.data.entity)
      this.companyData = res.data.entity
      this.centerDialogVisible1 = true
    },
    async add() {
      this.isEdit = false
      // this.dialogForm.companyName = this.bizOrgNo
      // this.dialogForm.companyNo = this.form.orgNo
      // const res = getCustomerInfoApi(this.tableData.id)
      console.log(this.tableData)
      // console.log(res)
      // this.getSelectiondata(this.form.orgNo)
      this.centerDialogVisible = true
      this.progress = 1
      // this.dialogForm = {}
      // const newTime = new Date()
      // console.log(newTime)
      // this.$set(this.dialogForm, 'createDate', newTime)
      // this.$refs.dialogForm?.resetFields()
    },
    next(progress, id) {
      this.progress = progress
      if (id) {
        this.id = id
      }
    },
    fileData(data) {
      this.companyData = data
      this.dizhi = data.companyAddress
    },
    beforeClose() {
      this.centerDialogVisible = false
      this.centerDialogVisible1 = false
      this.progress = 1
      this.id = ''
      this.isEdit = false
      this.isPreview = false
      this.companyData = {}
      this.dizhi = ''
      this.getdatalist('')
    },
    finish() {
      this.centerDialogVisible = false
      this.progress = 1
      this.id = ''
      this.getdatalist('')
    },
    // 删除
    async handleDel(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该档案, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delCustomerFileApi(id)
          this.$message.success(res.data.content)
          this.getdatalist(this.bizOrgNo)
        })
        .catch((action) => {
          if (action === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          }
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.option {
  height: auto;
}
.bigbox {
  background-color: #f1f1f1;
  padding: 10px;
  height: 100%;
  .topbox {
    background-color: #fff;
    padding: 20px 20px 0 0px;
    box-sizing: border-box;
    height: 80px !important;
    width: 100% !important;
    // transition-property: all;
    // transition-duration: 0.2s;
    ::v-deep .el-form-item {
      width: 100%;
      margin-left: -10px;
    }
    .button {
      width: 63px;
      height: 32px;
      margin-right: 5px;
      background-color: #fff;
      border: 1px solid #ccc;
      border-radius: 3px;
    }
    .chaxun {
      background-color: #1890ff;
      color: #fff;
    }
  }
  .bodybox {
    background-color: #fff;
    margin-top: 10px;
    padding: 20px 20px 20px 20px;
    height: 90%;
    .titlebox {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      .text {
        font-weight: bold;
        font-size: 18px;
      }
      .right {
        align-items: center;
        i {
          margin-left: 10px;
        }
        .bewdao {
          width: 63px;
          height: 32px;
        }
        .daochu {
          background-color: #1890ff;
          color: #fff;
          border: none;
          border-radius: 3px;
        }
      }
    }
    .fenye {
      margin-top: 15px;
      text-align: right;
    }
  }
}
::v-deep .el-form-item__content {
  width: calc(100% - 100px);
}
::v-deep .el-col-6 {
  height: 42px;
  text-align: right;
}
::v-deep .el-input__inner {
  height: 32px;
}
::v-deep.el-form--inline {
  height: 100% !important;
  box-sizing: border-box;
}
::v-deep .el-input__icon {
  line-height: 20px;
}

.dropdown-list {
  padding: 0;
  margin: 0;
  text-align: center;
  width: 50px;
}
.operate {
  display: flex;
  justify-content: space-around;
}
.dialogBtn {
  width: 80px;
  height: 40px;
  border-radius: 4px;
  font-size: 14px;
  text-align: center;
  align-items: center;
}
::v-deep.dialog {
  .el-dialog {
    width: 800px;
    // height: 550px;
    // padding-left: 10px;
    .el-dialog__header {
      padding: 20px 0;
      padding-left: 20px;
      font-size: 14px;
      text-align: left !important;
      border-bottom: 1px solid #f7f7f7;
    }
    // .el-dialog__body {
    //   line-height: 10px;
    //   padding-bottom: 15px;
    // }
    .el-dialog__footer {
      margin-top: 50px;
      height: 60px;
    }
  }
  //   .el-row {
  //     height: 55px;
  //     .el-col {
  //       height: 55px;
  //     }
  //   }
}

::v-deep.el-date-editor {
  .el-range-separator {
    line-height: 22px;
  }
}

::v-deep.dialog-content {
  .el-form-item__error {
    margin-top: -8px;
  }
  .el-form-item {
    height: 50px;
    .el-form-item__content {
      width: 90%;
      .el-select {
        width: 100%;
      }
    }
    .el-form-item__label {
      padding: 0;
      padding-right: 10px;
      padding-top: 3px;
      line-height: 32px;
    }
    margin-bottom: 5px;
  }
}
::v-deep.checkbox-item {
  border-top: 1px solid #f7f7f7;
  border-bottom: 1px solid #f7f7f7;
  margin-top: 20px;
  padding: 20px 0;
  .el-form-item {
    .el-form-item__content {
      width: 750px;
    }
  }
  .el-checkbox-group {
    .el-checkbox {
      margin-right: 0;
    }
  }
  .yewufanwei {
    padding-top: 10px;
  }
}

.map {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 500px;
}
button {
  cursor: pointer;
}
.top-line {
  display: flex;
  width: 100%;
  height: 80px;
  padding-bottom: 20px;
  margin-bottom: 30px;
  justify-content: space-between;
  line-height: 30px;
  align-items: center;
  border-bottom: 1px solid #bbb;
  .line {
    width: 12%;
    height: 1px;
    border: 1px solid #e0e0e0;
    background: #bbb;
  }
  .top-item {
    display: flex;
    .circle {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      border: 1px solid #bbb;
      color: #bbb;
      text-align: center;
      line-height: 30px;
      margin-right: 10px;
    }
    .text {
      font-weight: 800;
    }
  }
}
.checked-bgc {
  background-color: #1684fc;
  color: #fff !important;
  border: 1px solid #1684fc !important;
}
</style>
